import { findAllCategory } from '@/api/category'
import { topCategory } from '@/api/constants'

// 分类模块
export default {
  namespaced: true,
  state () {
    return {
      // 分类信息集合
      list: topCategory.map(item => ({ name: item })
      )
      // list: topCategory.map(item => {
      //   return { name: item }
      // })
    }
  },
  // 修改数据
  mutations: {
    show (state, item) {
      const obj = state.list.find(v => v.id === item.id)
      obj.open = true
    },
    hide (state, item) {
      const obj = state.list.find(v => v.id === item.id)
      obj.open = false
    },
    setList (state, payload) {
      state.list = payload
    }
  },
  // 异步
  actions: {
    async findList (store) {
      const { result } = await findAllCategory()
      // console.log(result)
      store.commit('setList', result.map(v => ({ ...v, open: false })))
    }
  }
}
